﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FileExplorer
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            //loadIcons();

            //IconData.Test(this);
            //String data = new ImageData().ToJson();
            //MessageBox.Show(data);

            DriverInfo driver = new DriverInfo();

            string data = driver.ToJson();
            driver.ShowIn(listView1);

            SystemDirTree tree = new SystemDirTree();
            string Str = tree.ToJson();
            new SystemDirTree().ShowIn(treeView1);
        }

        private void initTreeView(TreeView treeView1)
        {
            treeView1.Nodes.Clear();
            //treeView1.Nodes = new List<TreeNode>();
        }

        private ImageList largeImageList;

        /// <summary>
        /// 载入所需的Icon图像
        /// </summary>
        private void loadIcons()
        {
            if (largeImageList == null)
            {
                largeImageList = new ImageList();
                largeImageList.ColorDepth = ColorDepth.Depth32Bit;
                //largeImageList.ImageSize = SystemInformation.IconSize;
                largeImageList.ImageSize = new Size(24, 24);
                //largeImageList.ImageSize = new Size(32, 32);
            }

            if(!largeImageList.Images.ContainsKey("文件夹")) largeImageList.Images.Add("文件夹", SystemIcon.Get(3));
            if (!largeImageList.Images.ContainsKey("本地磁盘")) largeImageList.Images.Add("本地磁盘", SystemIcon.Get(8));
            if (!largeImageList.Images.ContainsKey("计算机")) largeImageList.Images.Add("计算机", SystemIcon.Get(15));
            if (!largeImageList.Images.ContainsKey("桌面")) largeImageList.Images.Add("桌面", SystemIcon.Get(34));
        }

        /// <summary>
        /// 添加文件对应的Icon信息到ImageList
        /// </summary>
        private int addFileIcon(string fullName)
        {
            if (!largeImageList.Images.ContainsKey(fullName)) largeImageList.Images.Add(fullName, FileIcon.getIcon2(fullName));
            return largeImageList.Images.IndexOfKey(fullName);
        }

        /// <summary>
        /// 在listView中显示
        /// </summary>
        public void ShowFolder(ListView lstView, String FullName)
        {
            lstView.View = View.Details;
            lstView.AllowColumnReorder = true;
            lstView.Columns.Clear();
            lstView.Items.Clear();

            lstView.LargeImageList = largeImageList;
            lstView.SmallImageList = largeImageList;

            lstView.Columns.Add("columnsName", "名称", 270);
            lstView.Columns.Add("columnsType", "类型", 120);
            lstView.Columns.Add("columnsMax", "修改日期", 120);
            lstView.Columns.Add("columnsFreeSpace", "大小", 80);

            DirectoryInfo dirs = new DirectoryInfo(FullName);

            //添加文件夹
            foreach (DirectoryInfo dir in dirs.GetDirectories())
            {
                ListViewItem item = new ListViewItem();
                item.Text = dir.Name;
                item.Tag = dir;
                item.ImageIndex = largeImageList.Images.IndexOfKey("文件夹");

                item.SubItems.Add("文件夹");
                item.SubItems.Add(dir.LastWriteTime.ToString("yyyy/MM/dd HH:mm"));
                item.SubItems.Add("");

                lstView.Items.Add(item);
            }

            //添加文件
            foreach (FileInfo file in dirs.GetFiles())
            {
                ListViewItem item = new ListViewItem();
                item.Text = file.Name;
                item.Tag = file;
                item.ImageIndex = addFileIcon(file.FullName);

                string type = GetExtsDescription(file.Extension);
                string size = getSize(file.Length);

                item.SubItems.Add(type);
                item.SubItems.Add(file.LastWriteTime.ToString("yyyy/MM/dd HH:mm"));
                item.SubItems.Add(size);

                lstView.Items.Add(item);
            }
        }

        /// <summary>
        /// 获取文件大小信息
        /// </summary>
        public string getSize(long FileLength)
        {
            double len = Math.Round((FileLength / 1024.0), 2);
            if (len < 1024) return (len + " KB");
            else
            {
                len = Math.Round((len / 1024.0), 2);
                if (len < 1024) return (len + " MB");
                else
                {
                    len = Math.Round((len / 1024.0), 2);
                    return (len + " GB");
                }
            }
        }

        /// <summary>
        /// 通过扩展名得到描述
        /// </summary>
        /// <param name="ext">扩展名，如.jpg</param>
        /// <param name="description">返回描述</param>
        public string GetExtsDescription(string ext)
        {
            string description = "";

            //从注册表中读取扩展名相应的子键
            RegistryKey extsubkey = Registry.ClassesRoot.OpenSubKey(ext);
            if (extsubkey == null)  //没有找到，那就是这种类型
            {
                description = ext.ToUpper().Substring(1) + "文件";
                return description;
            }

            //取出扩展名对应的文件类型名称  
            string extdefaultvalue = extsubkey.GetValue(null) as string;
            if (extdefaultvalue == null) return description;

            //扩展名类型是可执行文件
            if (extdefaultvalue.Equals("exefile", StringComparison.InvariantCultureIgnoreCase))
            {
                //从注册表中读取文件类型名称的相应子键  
                RegistryKey exefilesubkey = Registry.ClassesRoot.OpenSubKey(extdefaultvalue);
                if (exefilesubkey != null)  //如果不为空
                {
                    string exefiledescription = exefilesubkey.GetValue(null) as string;   //得到exefile描述字符串  
                    if (exefiledescription != null)
                    {
                        description = exefiledescription;
                    }
                }
                return description;
            }

            //从注册表中读取文件类型名称的相应子键  
            RegistryKey typesubkey = Registry.ClassesRoot.OpenSubKey(extdefaultvalue);
            if (typesubkey == null) return description;

            //得到类型描述字符串  
            string typedescription = typesubkey.GetValue(null) as string;
            if (typedescription != null) description = typedescription;

            return description;
        }

        private void label1_Click(object sender, EventArgs e)
        {
            String FullName = comboBox1.Text.Trim();
            //ShowFolder(listView1, FullName);

            DirInfo info = new DirInfo(FullName);
            String data = info.ToJson();

            info.ShowIn(listView1);
        }

    }
}
